// File Name: oled_task.c
// Author: kukukukuku~
// Date: 2024-07-04

#include "./User/Flash/inc/flash_task.h"
#include "./User/Key/inc/key_ctrl.h"
#include "./User/OLED/inc/oled_task.h"

TaskHandle_t taskHandleOled;    //OLED任务句柄

static uint8_t oledDisModeFlagLast; //上一次OLED显示主界面或设置界面标志
static uint8_t oledMainIdFlag;  //OLED主界面编号
static uint8_t oledMainIdCnt;   //OLED切换主界面编号计数

/**
 * @brief   OLED任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void task_oled(void *arg)
{
    vTaskDelay(50);
    
    //128x32
    oled_iic_send_data(0xAE, OLED_SEND_CMD);
    
    oled_iic_send_data(0x40, OLED_SEND_CMD);
    oled_iic_send_data(0xB0, OLED_SEND_CMD);
    
    if(oledLightLevel == 0)
    {
        oled_iic_send_data(0x81, OLED_SEND_CMD);
        oled_iic_send_data(0xFA, OLED_SEND_CMD);
    }
    else if(oledLightLevel == 1)
    {
        oled_iic_send_data(0x81, OLED_SEND_CMD);
        oled_iic_send_data(0x96, OLED_SEND_CMD);
    }
    else if(oledLightLevel == 2)
    {
        oled_iic_send_data(0x81, OLED_SEND_CMD);
        oled_iic_send_data(0x32, OLED_SEND_CMD);
    }
    
    if(oledDir == 0)
    {
        oled_iic_send_data(0xC8, OLED_SEND_CMD);
        oled_iic_send_data(0xA1, OLED_SEND_CMD);
    }
    else
    {
        oled_iic_send_data(0xC0, OLED_SEND_CMD);
        oled_iic_send_data(0xA0, OLED_SEND_CMD);
    }
    
    oled_iic_send_data(0xA6, OLED_SEND_CMD);
    
    oled_iic_send_data(0xA8, OLED_SEND_CMD);
    oled_iic_send_data(0x1F, OLED_SEND_CMD);
    
    oled_iic_send_data(0xD3, OLED_SEND_CMD);
    oled_iic_send_data(0x00, OLED_SEND_CMD);
    
    oled_iic_send_data(0xD5, OLED_SEND_CMD);
    oled_iic_send_data(0xF0, OLED_SEND_CMD);
    
    oled_iic_send_data(0xD9, OLED_SEND_CMD);
    oled_iic_send_data(0x22, OLED_SEND_CMD);
    
    oled_iic_send_data(0xDA, OLED_SEND_CMD);
    oled_iic_send_data(0x02, OLED_SEND_CMD);
    
    oled_iic_send_data(0xDB, OLED_SEND_CMD);
    oled_iic_send_data(0x49, OLED_SEND_CMD);
    
    oled_iic_send_data(0x8D, OLED_SEND_CMD);
    oled_iic_send_data(0x14, OLED_SEND_CMD);
    
    oled_iic_send_data(0xAF, OLED_SEND_CMD);
    
    //128x64
//    oled_iic_send_data(0xAE, OLED_SEND_CMD);   //显示屏休眠
//    
//    oled_iic_send_data(0xFD,OLED_SEND_CMD);
//    oled_iic_send_data(0x12,OLED_SEND_CMD);
//    oled_iic_send_data(0xAE,OLED_SEND_CMD);//--turn off oled panel
//    oled_iic_send_data(0xd5,OLED_SEND_CMD);//--set display clock divide ratio/oscillator frequency
//    oled_iic_send_data(0xA0,OLED_SEND_CMD);
//    oled_iic_send_data(0xA8,OLED_SEND_CMD);//--set multiplex ratio(1 to 64)
//    oled_iic_send_data(0x3f,OLED_SEND_CMD);//--1/64 duty
//    oled_iic_send_data(0xD3,OLED_SEND_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
//    oled_iic_send_data(0x00,OLED_SEND_CMD);//-not offset

//    oled_iic_send_data(0x40,OLED_SEND_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
//    oled_iic_send_data(0xA1,OLED_SEND_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
//    oled_iic_send_data(0xC8,OLED_SEND_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
//    oled_iic_send_data(0xDA,OLED_SEND_CMD);//--set com pins hardware configuration
//    oled_iic_send_data(0x12,OLED_SEND_CMD);
//    oled_iic_send_data(0x81,OLED_SEND_CMD);//--set contrast control register
//    oled_iic_send_data(0xFA,OLED_SEND_CMD);// Set SEG Output Current Brightness
//    oled_iic_send_data(0xD9,OLED_SEND_CMD);//--set pre-charge period
//    oled_iic_send_data(0x82,OLED_SEND_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
//    oled_iic_send_data(0xDB,OLED_SEND_CMD);//--set vcomh
//    oled_iic_send_data(0x34,OLED_SEND_CMD);//Set VCOM Deselect Level
//    oled_iic_send_data(0xA4,OLED_SEND_CMD);// Disable Entire Display On (0xa4/0xa5)
//    oled_iic_send_data(0xA6,OLED_SEND_CMD);// Disable Inverse Display On (0xa6/a7)
//    
//    oled_iic_send_data(0xAF, OLED_SEND_CMD);
    oled_clear();
    
    if(flashReadFlag == 0)
    {
        oled_show_char_8x16(0, 1, '!', 1);
        oled_show_char_8x16(120, 1, '!', 1);
        oled_show_str_8x16(36, 1, (uint8_t *)"Read ok", 0);
    }
    else
    {
        oled_show_char_8x16(0, 1, '!', 1);
        oled_show_char_8x16(120, 1, '!', 1);
        oled_show_str_8x16(28, 1, (uint8_t *)"Read fail", 0);
    }
    
    vTaskDelay(1000);
    
    oled_clear();
    oled_dis_main(0);
    
    while(1)
    {
        //主界面和设置界面切换
        if(oledDisModeFlagLast != oledDisModeFlag)
        {
            oled_clear();
            
            if(oledDisModeFlag == 0)
            {
                oled_dis_main(oledMainIdFlag);
            }
            
            oledMenuLevel = 0;
            oledDisModeFlagLast = oledDisModeFlag;
        }
        
        //设置界面
        if(oledDisModeFlag)
        {
            oledMainIdCnt = 0;
            
            switch(oledMenuLevel)
            {
                //一级菜单
                case 0:
                    oledSelectLevel1 = oled_dis_menu(&oledMenuOption);
                    
                    //Save
                    if(oledSelectLevel1 == OLED_LEVEL1_SAVE_INDEX)
                    {
                        xSemaphoreGive(semaphBinFlash);
                    }
                    
                    break;
                //二级菜单
                case 1:
                    //匹配一级菜单编号
                    switch(oledSelectLevel1)
                    {
                        //Key
                        case OLED_LEVEL1_KEY_INDEX:
                            oledSelectLevel2 = oled_dis_menu(&oledMenuKey);
                            
                            //按键锁定
                            if(oledSelectLevel2 == OLED_LEVEL2_KEY_LOCK_INDEX)
                            {
                                keyLockFlag = 1;
                                oledDisModeFlag = 0;
                            }
                            
                            break;
                        //LED
                        case OLED_LEVEL1_LED_INDEX:
                            oledSelectLevel2 = oled_dis_menu(&oledMenuLed);
                            break;
                        //OLED
                        case OLED_LEVEL1_OLED_INDEX:
                            oledSelectLevel2 = oled_dis_menu(&oledMenuOled);
                            
                            //关闭OLED显示
                            if(oledSelectLevel2 == OLED_LEVEL2_OLED_OFF_INDEX)
                            {
                                oled_display_off();
                                oledOffFlag = 1;
                            }
                            
                            break;
                        //Step motor
                        case OLED_LEVEL1_STEP_MOTOR_INDEX:
                            oledSelectLevel2 = oled_dis_menu(&oledMenuStepMotor);
                            break;
                        //WS2812B
                        case OLED_LEVEL1_WS2812B_INDEX:
                            oledSelectLevel2 = oled_dis_menu(&oledMenuWs2812b);
                            break;
                        default:
                            break;
                    }
                    break;
                //三级菜单
                case 2:
                    //匹配一级菜单编号
                    switch(oledSelectLevel1)
                    {
                        //Key
                        case OLED_LEVEL1_KEY_INDEX:
                            oled_menu_level3_key();
                            break;
                        //LED
                        case OLED_LEVEL1_LED_INDEX:
                            oled_menu_level3_led();
                            break;
                        //OLED
                        case OLED_LEVEL1_OLED_INDEX:
                            oled_menu_level3_oled();
                            break;
                        //Step motor
                        case OLED_LEVEL1_STEP_MOTOR_INDEX:
                            oled_menu_level3_step_motor();
                            break;
                        //WS2812B
                        case OLED_LEVEL1_WS2812B_INDEX:
                            oled_menu_level3_ws2812b();
                            break;
                        default:
                            break;
                    }
                    break;
                //四级菜单
                case 3:
                    //匹配一级菜单编号
                    switch(oledSelectLevel1)
                    {
                        //LED
                        case OLED_LEVEL1_LED_INDEX:
                            oled_menu_level4_led();
                            break;
                        //WS2812B
                        case OLED_LEVEL1_WS2812B_INDEX:
                            oled_menu_level4_ws2812b();
                            break;
                        default:
                            break;
                    }
                    break;
                //五级菜单
                case 4:
                    //匹配一级菜单编号
                    switch(oledSelectLevel1)
                    {
                        //WS2812B
                        case OLED_LEVEL1_WS2812B_INDEX:
                            oled_menu_level5_ws2812b();
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
        //主界面 每秒刷新一次
        else if(++oledMainIdCnt >= 100)
        {
            oledMainIdCnt = 0;
            
            if(++oledMainIdFlag >= 14)
            {
                oledMainIdFlag = 0;
            }
            
            oled_clear();
            oled_dis_main(oledMainIdFlag);
        }
        
        //如果关闭OLED, 则挂起OLED任务
        if(oledOffFlag)
        {
            oledDisModeFlag = 0;
            vTaskSuspend(NULL);
        }
        //如果OLED被唤醒，则显示以唤醒提示
        else if(oledWarnOledAwakenedFlag)
        {
            oled_clear();
            oled_show_char_8x16(0, 1, '!', 1);
            oled_show_char_8x16(120, 1, '!', 1);
            oled_show_str_8x16(32, 1, (uint8_t *)"Awakened", 0);
            oledWarnOledAwakenedFlag = 0;
            
            vTaskDelay(1000);
            
            oledMainIdCnt = 0;
            oledMainIdFlag = 0;
            oled_clear();
            oled_dis_main(oledMainIdFlag);
        }
        //如果按键锁定，则显示已锁定提示
        else if(oledWarnKeyLockedFlag)
        {
            oled_clear();
            oled_show_char_8x16(0, 1, '!', 1);
            oled_show_char_8x16(120, 1, '!', 1);
            oled_show_str_8x16(24, 1, (uint8_t *)"Key locked", 0);
            oledWarnKeyLockedFlag = 0;
            vTaskDelay(1000);
        }
        //如果按键解锁，则显示已解锁提示
        else if(oledWarnKeyUnlockedFlag)
        {
            oled_clear();
            oled_show_char_8x16(0, 1, '!', 1);
            oled_show_char_8x16(120, 1, '!', 1);
            oled_show_str_8x16(16, 1, (uint8_t *)"Key unlocked", 0);
            oledWarnKeyUnlockedFlag = 0;
            vTaskDelay(1000);
        }
        
        if(flashSaveFlag == 1)
        {
            oled_clear();
            oled_show_char_8x16(0, 1, '!', 1);
            oled_show_char_8x16(120, 1, '!', 1);
            oled_show_str_8x16(36, 1, (uint8_t *)"Save ok", 0);
            vTaskDelay(1000);
        }
        else if(flashSaveFlag == 2)
        {
            oled_clear();
            oled_show_char_8x16(0, 1, '!', 1);
            oled_show_char_8x16(120, 1, '!', 1);
            oled_show_str_8x16(28, 1, (uint8_t *)"Save fail", 0);
            vTaskDelay(1000);
        }
        
        flashSaveFlag = 0;
        
        vTaskDelay(10);
    }
}
